package hit.example.home;

import hit.example.Db.DbAnhViet;
import hit.example.Db.DbRecentFav;
import hit.example.Object.ObjWord;
import hit.example.adapter.AdapterListView;
import hit.example.hitdict.R;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.telpoo.frame.model.BaseModel;

public class ListWordFm extends MyFragment implements OnItemClickListener {

	ListView lv;

	DbAnhViet databaseAnhViet;
	AdapterListView adpListView;
	ArrayList<ObjWord> listWord;
	BaseModel baseModel;
	TextView txtInfo,txtMarque;

	public ListWordFm() {
		super();
	}

	public static ListWordFm getInstance() {
		ListWordFm fm = new ListWordFm();
		return fm;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		return inflater.inflate(R.layout.fm_listword, container, false);
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		lv = (ListView) view.findViewById(R.id.lvDanhSachtu);
		txtInfo = (TextView) view.findViewById(R.id.txtInfo);
		txtMarque=(TextView)view.findViewById(R.id.txtMarque);
		txtMarque.setSelected(true);
		adpListView = new AdapterListView(getActivity(),
				R.layout.custom_listview_item, new ArrayList<ObjWord>());
		lv.setAdapter(adpListView);
		lv.setOnItemClickListener(this);
		HomeActivity.getInstance().setFont(txtInfo, getActivity());
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		setupTask();
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
		DbRecentFav db = new DbRecentFav(getActivity());
		db.createRecent(adpListView.getItem(pos));
		HomeActivity.getInstance().pushDetail(adpListView.getItem(pos),-1);
		HomeActivity.getInstance().hiddenInput();
		HomeActivity.getInstance().setEditText(adpListView.getItem(pos).getiWord());

	}

	public void setupTask() {
		baseModel = new BaseModel() {

			@Override
			public void onSuccess(int taskType, ArrayList<?> list, String msg) {
				super.onSuccess(taskType, list, msg);

				switch (taskType) {
				case 1:
					if (listWord == null)
						return;
					listWord.clear();
					listWord = (ArrayList<ObjWord>) list;
					adpListView.setItems(listWord);
					adpListView.notifyDataSetChanged();

					break;

				default:
					break;
				}

			}

			@Override
			public void onFail(int taskType, String msg) {
				super.onFail(taskType, msg);
			}
		};

	}
	private static long back_pressed;

	@Override
	public boolean onBackPressed() {
		
		if (back_pressed + 2000 > System.currentTimeMillis()) getActivity().finish();
        else showToast("Press once again to exit!");
        back_pressed = System.currentTimeMillis();

		return true;
	}
	
	/*
	 * public void openSeach(String key) {
	 * 
	 * ArrayList<String> arrsend = new ArrayList<String>(); arrsend.add(key);
	 * TaskDinh db = new TaskDinh(baseModel, 1, arrsend, getActivity());
	 * baseModel.exeTask(null, db); }
	 */

	@Override
	public void updateNewData(ArrayList<ObjWord> ojs, int pos) {
		super.updateNewData(ojs, pos);
		adpListView.setItems(ojs);
		adpListView.notifyDataSetChanged();
	}

}
